﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LYQ.BLL;
using WebManager;

namespace BaoJian.WebManager.Manager.KeyWord
{
    public partial class NewKey : System.Web.UI.Page
    {  
        /// <summary>
        /// 栏目ID
        /// </summary>
        private string ColumnID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];
            if (ColumnID == null)
                return;

        }

        protected void Save_Click(object sender, EventArgs e)
        {
            LYQ.BLL.Keyword bll = new  Keyword();
            string[] k = Keys.Text.Replace("\r\n", "#").Split('#');
            string[] level = KLevels.Text.Trim().Replace("\r\n", "#").Split('#');
            if (k.Length != level.Length)
                return;
            for (int i = 0; i < k.Length;i++ )
            {
                if(bll.Exists(k[i].Trim()))
                {
                    Msg.Text += k[i] + " ";
                    continue;
                }
                LYQ.Model.Keyword model = new LYQ.Model.Keyword();
                model.KLevel = Convert.ToInt32(level[i]);
                model.KWord = k[i].Trim();
                model.CDate = DateTime.Now;
                model.UDate = DateTime.Now;
                model.HitCount = 0;
                model.RepeatCount = 0;
                model.Description = "";
                model.RelatedColList = "";
                model.RelatedDocKey = "";
                model.RelatedAdKey = "";
                bll.Add(model);
            }

            if(Msg.Text != string.Empty)
            {
                Msg.Text += " 等关键字已经存在，请返回";
                Msg.Visible = true;
            }
            else
            {
                ReturnToList();
            }
            
       
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            ReturnToList();
        }

        private  void ReturnToList()
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/KeyWord/List.aspx?OID=" + ColumnID;
            Page.Response.Redirect(url);
        }
    }
}
